package Main;
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import Objetos.Objeto;
import Personajes.Mario;
import Logica_Moviemientos.*;
import Mapa.*;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Juego extends javax.swing.JFrame {
	private JPanel PanelObjetos;
	private Mapa mapa;
	private JPanel PanelPersonajes;
	private Mario mario;
	private boolean saltoPresionado;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Juego inst = new Juego();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public Juego() {
		super();
		initGUI();
		this.setExtendedState(this.MAXIMIZED_BOTH);
	}
	
	private void initGUI() {
		try {
			saltoPresionado = false;
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			getContentPane().setLayout(null);
			getContentPane().setBackground(new java.awt.Color(128,128,255));
			{	
				PanelPersonajes = new JPanel();
				getContentPane().add(PanelPersonajes, BorderLayout.CENTER);
				PanelPersonajes.setOpaque(false);
				PanelPersonajes.setLayout(null);
				PanelPersonajes.setBounds(0, 0, 1200, 800);
				PanelPersonajes.setIgnoreRepaint(false);
			}
			{
				PanelObjetos = new JPanel();
				getContentPane().add(PanelObjetos, BorderLayout.CENTER);
				PanelObjetos.setLayout(null);
				PanelObjetos.setSize(9600, 800);
				PanelObjetos.setPreferredSize(new java.awt.Dimension(1200, 800));
				PanelObjetos.setOpaque(false);
			}
			pack();
			this.setSize(800, 700);
			addKeyListener(new OyenteMario());
			cargarGraficos();
			cargarMario();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**Agrega el mario al juego. */
	private void cargarMario(){
		mario = mapa.obtenerMario();
		Inteligencia_Manual i = new Inteligencia_Manual(mapa,mario,new Actualizador(PanelObjetos,mapa));
		mario.setInteligencia(i);
		PanelPersonajes.add(mario);
		Thread t = new Thread(mario);
		t.start();
	}
	
	/**Carga los los objetos al juego. */
	private void cargarGraficos(){
		mapa = new Mapa();
		for(Objeto o : mapa.obtenerObjetos()){
			PanelObjetos.add(o);
		}
	}
	
	private class OyenteMario extends KeyAdapter{
		public void keyPressed(KeyEvent e){
			if(e.getKeyCode() == KeyEvent.VK_LEFT){
				mario.setIzquierda(true);
			}
			else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
				mario.setDerecha(true);
			}
			if(e.getKeyCode() == KeyEvent.VK_UP){
				if(!saltoPresionado){
					saltoPresionado = true;
					mario.setSalto(true);
				}
			}
		}
		
		public void keyReleased(KeyEvent e){
			if(e.getKeyCode() == KeyEvent.VK_LEFT){
				mario.setIzquierda(false);
			}
			else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
				mario.setDerecha(false);
			}
			if(e.getKeyCode() == KeyEvent.VK_UP){
				saltoPresionado = false;
				mario.setSalto(false);
			}
		}
	}
	
}
